package com.ruoyi.huanjing.controller;

import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DemensionController {
    public static void main(String[] args) {
        String filePath = "E:\\localfile\\MYD021KM.A2017314.0345.061.2017314164621.hdf";
        List<Map<String, String>> variableList = new ArrayList<>();
        int id = 1; // 递增ID计数器

        try (NetcdfFile ncFile = NetcdfFile.open(filePath)) {
            List<Variable> variables = ncFile.getVariables();

            for (Variable var : variables) {
                Map<String, String> variableInfo = new HashMap<>();
                variableInfo.put("id",  String.valueOf(id++));
                variableInfo.put("variable", var.getFullName());

                // 获取变量的维度信息
                List<ucar.nc2.Dimension> dimensions = var.getDimensions();
                // 过滤掉可能的无效维度，这里假设字符型变量不应有有效维度
                if (var.getDataType().isString()) {
                    dimensions = new ArrayList<>();
                }
                // 新增：若为结构体类型，将维度列表置空
                if (var instanceof ucar.nc2.Structure) {
                    dimensions = new ArrayList<>();
                }
                int dimCount = dimensions.size();
                StringBuilder dimInfo = new StringBuilder();
                for (int i = 0; i < dimensions.size(); i++) {
                    if (i > 0) {
                        dimInfo.append(", ");
                    }
                    ucar.nc2.Dimension dim = dimensions.get(i);
                    dimInfo.append(dim.getShortName());
                }

                // 判断变量类型，没有维度时 type 设为 "-" 
                String varType;
                if (dimCount == 0 || dimensions.stream().anyMatch(d -> d.getLength() == 1)) {
                    varType = "-";
                } else {
                    // 仅当变量同时包含 "2*nscans" 和 "1KM_geo_dim" 维度时才判定为 Geo2D 类型
                    boolean has2NScans = dimensions.stream().anyMatch(d -> d.getShortName() != null && d.getShortName().equals("2*nscans") || d.getShortName().equals("lat"));
                    boolean has1KMGeoDim = dimensions.stream().anyMatch(d -> d.getShortName() != null && d.getShortName().equals("1KM_geo_dim") || d.getShortName().equals("lat"));
                    if (has2NScans && has1KMGeoDim) {
                        varType = "Geo2D";
                    } else {
                        // 假设只有两个独立有效维度时判定为2D
                        // 这里需要根据实际情况调整判断逻辑，当前为示例
                        if (dimCount >= 2) {
                            varType = "2D";
                        } else {
                            varType = dimCount + "D";
                        }
                    }
                }
                variableInfo.put("type", varType);

                if (!dimensions.isEmpty()) {
                    variableInfo.put("dimensions", dimInfo.toString());
                }
                variableList.add(variableInfo);
            }

        } catch (IOException e) {
            System.err.println("读取变量列表时出错: " + e.getMessage());
            e.printStackTrace();
            // 出错时返回空列表
        }
        variableList.stream().forEach(System.out::println);
    }
}
